<?php
	require_once (dirname(__FILE__)."/../config/Config.inc");
	require_once (dirname(__FILE__)."/../config/api.inc");
	$result['status'] = '0';
	
	//忘记密码流程
	$phone =  getParam('phone','get');
	$code =  getParam('code','get');
	$pwd =  getParam('pwd','get');
	
	if(empty($phone))
	{
		$result['msg'] = '请输入手机号';
		echoJSON($result);
		exit;
	}
	
	if(empty($code))
	{
		$result['msg'] = '请输入验证码';
		echoJSON($result);
		exit;
	}
	
	if(empty($pwd))
	{
		$result['msg'] = '请输入密码';
		echoJSON($result);
		exit;
	}
	
	unset($where);
	
	$where['phone'] = $phone;
	
	if($uservalid = _User::getUserValidCode($where))
	{
		if($uservalid['errornums'] >= 5)
		{
			$result['msg'] = '验证码错误次数过多';
			echoJSON($result);
			exit;
		}
		
		if($uservalid['code'] == $code)
		{
			//删除验证码
			unset($query);
			$query['phone'] = $phone;
			_User::delUserValidCode($query);
			
			unset($query);
	
			$query['loginname'] = $phone;
			
			if($userinfo = _User::getUserInfo($query))
			{
				unset($query,$data);
				
				$query['id'] = $userinfo['id'];
				
				$data['password'] = md5($pwd); 
				
				$res = _user::setUserInfo($query, $data);
				if(!empty($res))
				{
					//删除验证码
					unset($query);
					$query['phone'] = $phone;
					_User::delUserValidCode($query);
					
					$result['msg'] = '重置密码成功';
					$result['status'] = '1';
					//$result['url'] = '?user.php?a=login';//跳转到登陆页面
					echoJSON($result);
				}
				else
				{
					$result['msg'] = '重置密码失败';
				}
			}
			else
			{
				$result['msg'] = '系统异常';
				echoJSON($result);
				exit;
			}
		}
		else
		{
			unset($query,$data);
			$query['id'] = $uservalid['id'];
			$data['errornums'] = $uservalid['errornums']+1;
			
			_User::setUserValidCode($query, $data);
			$result['msg'] = '验证码输入错误';
			echoJSON($result);
			exit;
		}
	}
	else
	{
		$result['msg'] = '请先获取验证码';
		echoJSON($result);
		exit;
	}
	
	